#include "Message.h"

static int i=0;

Message::Message() {
//	cout<<"creating msg "<<i++<<endl;
}

Message::Message(string msg, int sender, int reciver, int type) :
	sender(sender), reciver(reciver), type(type), ballCounter(0), history(),
			kill(false), revive(false), exit(false) {
	this->msg = msg;
	this->RTmsg = NULL;

//	cout<<"creating msg "<<i++<<endl;
}

Message::Message(map<int, pair<int, int> > * msg, int sender, int reciver,
		int type) :
	sender(sender), reciver(reciver), type(type), ballCounter(0), history(),
			kill(false), revive(false), exit(false) {
	this->msg = "";
	this->RTmsg = msg;
//	cout<<"creating msg "<<i++<<endl;

}

Message::Message(const Message& other) {
//	cout<<"-----------COPY CTOR------------------"<<endl;
	this->msg = other.msg;
	this->sender = other.sender;
	this->reciver = other.reciver;
	this->type = other.type;
	this->RTmsg = other.RTmsg;
	this->history = other.history;
	this->ballCounter = other.ballCounter;
	this->kill = other.kill;
	this->revive = other.revive;
	this->exit = other.exit;
//	cout<<"creating msg "<<i++<<endl;
}

Message::~Message() {
	i--;
//	cout<<"removing msg "<<i<<endl;
	if (this->RTmsg)
		delete RTmsg;

}

string Message::toString() {
	stringstream ret;
	ret << "Message:" << endl;
	ret << "Type: " << this->type << endl;
	ret << "sender: " << this->sender << " | reciver: " << this->reciver
			<< endl;
	if (this->type == Message::RT)
		ret << "RT: " << endl << printRT() << endl;
	else
		ret << "message: " << this->msg << endl;
	ret << "message history: ";
	int i = 0;

	for (i = 0; i < this->history.size(); i++) {
		if (history[i])
			ret << history[i] << " -> ";
	}
	ret << endl << "balled around for: " << this->ballCounter << " times"
			<< endl;
	return ret.str();
}

string Message::printRT() {
	stringstream ret;
	if (this->RTmsg->empty())
		ret << "EMTY RT!!!" << endl;
	for (map<int, pair<int, int> >::iterator i = RTmsg->begin(); i
			!= RTmsg->end(); i++) {
		ret << i->first << " : " << i->second.first << " , "
				<< i->second.second << endl;
	}
	return ret.str();
}

